﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(ImageI18N))]
    public class ImageI18NEditor : Editor
    {
        protected SerializedProperty keyid;
        protected SerializedProperty binds;
        protected ReorderableList reorderableList;
        protected SerializedProperty TranslateLanguage;

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            if (this.keyid != null)
            {
                EditorGUILayout.Space(5f);
                EditorGUILayout.PropertyField(this.keyid, new GUILayoutOption[0]);
                EditorGUILayout.Space(5f);
            }
            reorderableList.DoLayoutList();
            serializedObject.ApplyModifiedProperties();


            EditorGUILayout.BeginVertical();
            EditorGUILayout.PropertyField(this.TranslateLanguage, new GUILayoutOption[0]);

            if (GUILayoutEx.ActionButton("Translate", new GUILayoutOption[0]))
            {
                base.serializedObject.Update();
                Translatable translate = (Translatable)this.target;
                translate.TestSwitchLanugage(this.TranslateLanguage.intValue);
                base.serializedObject.ApplyModifiedProperties();
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.Space(5f);
            serializedObject.ApplyModifiedProperties();
        }

        protected virtual void OnEnable()
        {
            if (this.target == null) return;
            this.TranslateLanguage = serializedObject.FindProperty("TranslateLanguage");
            this.binds = serializedObject.FindProperty("binds");
            this.keyid = serializedObject.FindProperty("keyid");

            this.reorderableList = new ReorderableList(serializedObject, this.binds);
            this.reorderableList.drawHeaderCallback = OnDrawHead;
            this.reorderableList.elementHeight = EditorGUIUtility.singleLineHeight;
            this.reorderableList.drawElementCallback = OnDrawElement;
            this.reorderableList.onAddCallback = OnAddElement;
            
        }

        protected void OnDrawHead(Rect rect)
        {
            Rect position = new Rect(rect.x + 15f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect position2 = new Rect(rect.x + 15f + rect.width / 2f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            GUI.Label(position, "Language");
            GUI.Label(position2, "Translate");
        }
        protected void OnDrawElement(Rect rect, int index, bool isActive, bool isForce)
        {
            SerializedProperty arrayElementAtIndex = this.binds.GetArrayElementAtIndex(index);

            SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
            SerializedProperty translate = arrayElementAtIndex.FindPropertyRelative("translate");

            Rect recttime = new Rect(rect.x, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect recttime2 = new Rect(rect.x + rect.width / 4f+6f, rect.y, rect.width - rect.width / 4f, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(recttime, languagetype, GUIContent.none);
            EditorGUI.PropertyField(recttime2, translate, GUIContent.none);
        }


        protected void OnAddElement(ReorderableList list)
        {
            if (list.serializedProperty != null)
            {
                list.serializedProperty.arraySize++;
                list.index = list.serializedProperty.arraySize - 1;

                SerializedProperty arrayElementAtIndex = list.serializedProperty.GetArrayElementAtIndex(list.index);
                SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
                SerializedProperty translate = arrayElementAtIndex.FindPropertyRelative("translate");
                languagetype.enumValueIndex = 0;
                translate.objectReferenceValue = null;
            }
            else
            {
                ReorderableList.defaultBehaviours.DoAddButton(list);
            }
        }

    }
}

